use crate::src::src::binary_heap::{
    binary_heap_free, binary_heap_insert, binary_heap_new, binary_heap_num_entries, binary_heap_pop,
};
use crate::src::src::compare_int::int_compare;
use crate::src::struct_and_type::{BinaryHeap, BinaryHeapType, BinaryHeapValue, UnitTestFunction};
use crate::src::test::alloc_testing::alloc_test_set_limit;
use crate::src::test::framework::run_tests;
use :: libc;
extern "C" {}
pub const BINARY_HEAP_TYPE_MAX: BinaryHeapType = 1;
pub const BINARY_HEAP_TYPE_MIN: BinaryHeapType = 0;
#[no_mangle]
pub static mut test_array_test_binary_heap: [i32; 10000] = [0; 10000];
#[no_mangle]
pub extern "C" fn test_binary_heap_new_free() {
    let mut heap: *mut BinaryHeap = 0 as *mut BinaryHeap;
    let mut i: i32 = 0;
    i = 0 as i32;
    while i < 10000 as i32 {
        heap = binary_heap_new(
            BINARY_HEAP_TYPE_MIN,
            Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
        );
        binary_heap_free(heap);
        i += 1;
        i;
    }
    alloc_test_set_limit(0 as i32);
    heap = binary_heap_new(
        BINARY_HEAP_TYPE_MIN,
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    assert!(heap.is_null());
    alloc_test_set_limit(1 as i32);
    heap = binary_heap_new(
        BINARY_HEAP_TYPE_MIN,
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    assert!(heap.is_null());
}
#[no_mangle]
pub extern "C" fn test_binary_heap_insert() {
    let mut heap: *mut BinaryHeap = 0 as *mut BinaryHeap;
    let mut i: i32 = 0;
    heap = binary_heap_new(
        BINARY_HEAP_TYPE_MIN,
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    i = 0 as i32;
    while i < 10000 as i32 {
        (unsafe { test_array_test_binary_heap[i as usize] = i });
        assert!(
            binary_heap_insert(
                heap,
                (unsafe { &mut *test_array_test_binary_heap.as_mut_ptr().offset(i as isize) })
                    as *mut i32 as BinaryHeapValue,
            ) != 0 as i32
        );
        i += 1;
        i;
    }
    assert!(binary_heap_num_entries(heap) == 10000 as i32 as u32);
    binary_heap_free(heap);
}
#[no_mangle]
pub extern "C" fn test_min_heap_test_binary_heap() {
    let mut heap: *mut BinaryHeap = 0 as *mut BinaryHeap;
    let mut val: *mut i32 = 0 as *mut i32;
    let mut i: i32 = 0;
    heap = binary_heap_new(
        BINARY_HEAP_TYPE_MIN,
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    i = 0 as i32;
    while i < 10000 as i32 {
        (unsafe { test_array_test_binary_heap[i as usize] = i });
        assert!(
            binary_heap_insert(
                heap,
                (unsafe { &mut *test_array_test_binary_heap.as_mut_ptr().offset(i as isize) })
                    as *mut i32 as BinaryHeapValue,
            ) != 0 as i32
        );
        i += 1;
        i;
    }
    i = -(1 as i32);
    while binary_heap_num_entries(heap) > 0 as i32 as u32 {
        val = binary_heap_pop(heap) as *mut i32;
        assert!((unsafe { *val }) == i + 1 as i32);
        i = unsafe { *val };
    }
    assert!(binary_heap_num_entries(heap) == 0 as i32 as u32);
    assert!((binary_heap_pop(heap)).is_null());
    binary_heap_free(heap);
}
#[no_mangle]
pub extern "C" fn test_max_heap_test_binary_heap() {
    let mut heap: *mut BinaryHeap = 0 as *mut BinaryHeap;
    let mut val: *mut i32 = 0 as *mut i32;
    let mut i: i32 = 0;
    heap = binary_heap_new(
        BINARY_HEAP_TYPE_MAX,
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    i = 0 as i32;
    while i < 10000 as i32 {
        (unsafe { test_array_test_binary_heap[i as usize] = i });
        assert!(
            binary_heap_insert(
                heap,
                (unsafe { &mut *test_array_test_binary_heap.as_mut_ptr().offset(i as isize) })
                    as *mut i32 as BinaryHeapValue,
            ) != 0 as i32
        );
        i += 1;
        i;
    }
    i = 10000 as i32;
    while binary_heap_num_entries(heap) > 0 as i32 as u32 {
        val = binary_heap_pop(heap) as *mut i32;
        assert!((unsafe { *val }) == i - 1 as i32);
        i = unsafe { *val };
    }
    binary_heap_free(heap);
}
#[no_mangle]
pub extern "C" fn test_out_of_memory_test_binary_heap() {
    let mut heap: *mut BinaryHeap = 0 as *mut BinaryHeap;
    let mut value: *mut i32 = 0 as *mut i32;
    let mut values: [i32; 16] = [
        15 as i32, 14 as i32, 13 as i32, 12 as i32, 11 as i32, 10 as i32, 9 as i32, 8 as i32,
        7 as i32, 6 as i32, 5 as i32, 4 as i32, 3 as i32, 2 as i32, 1 as i32, 0 as i32,
    ];
    let mut i: i32 = 0;
    heap = binary_heap_new(
        BINARY_HEAP_TYPE_MIN,
        Some(int_compare as unsafe extern "C" fn(*mut libc::c_void, *mut libc::c_void) -> i32),
    );
    alloc_test_set_limit(0 as i32);
    i = 0 as i32;
    while i < 16 as i32 {
        assert!(
            binary_heap_insert(
                heap,
                (unsafe { &mut *values.as_mut_ptr().offset(i as isize) }) as *mut i32
                    as BinaryHeapValue,
            ) != 0 as i32
        );
        i += 1;
        i;
    }
    assert!(binary_heap_num_entries(heap) == 16 as i32 as u32);
    i = 0 as i32;
    while i < 16 as i32 {
        assert!(
            binary_heap_insert(
                heap,
                (unsafe { &mut *values.as_mut_ptr().offset(i as isize) }) as *mut i32
                    as BinaryHeapValue,
            ) == 0 as i32
        );
        assert!(binary_heap_num_entries(heap) == 16 as i32 as u32);
        i += 1;
        i;
    }
    i = 0 as i32;
    while i < 16 as i32 {
        value = binary_heap_pop(heap) as *mut i32;
        assert!((unsafe { *value }) == i);
        i += 1;
        i;
    }
    assert!(binary_heap_num_entries(heap) == 0 as i32 as u32);
    binary_heap_free(heap);
}
static mut tests_test_binary_heap: [UnitTestFunction; 6] = {
    [
        Some(test_binary_heap_new_free as unsafe extern "C" fn() -> ()),
        Some(test_binary_heap_insert as unsafe extern "C" fn() -> ()),
        Some(test_min_heap_test_binary_heap as unsafe extern "C" fn() -> ()),
        Some(test_max_heap_test_binary_heap as unsafe extern "C" fn() -> ()),
        Some(test_out_of_memory_test_binary_heap as unsafe extern "C" fn() -> ()),
        None,
    ]
};
fn main_0_test_binary_heap(mut _argc: i32, mut _argv: *mut *mut i8) -> i32 {
    run_tests(unsafe { tests_test_binary_heap.as_mut_ptr() });
    return 0 as i32;
}
pub fn main_test_binary_heap() {
    let mut args: Vec<*mut i8> = Vec::new();
    for arg in ::std::env::args() {
        args.push(
            (::std::ffi::CString::new(arg))
                .expect("Failed to convert argument into CString.")
                .into_raw(),
        );
    }
    args.push(::core::ptr::null_mut());
    {
        ::std::process::exit(main_0_test_binary_heap(
            (args.len() - 1) as i32,
            args.as_mut_ptr() as *mut *mut i8,
        ) as i32)
    }
}
